package com.hzpd.hainan.module.newspager.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hzpd.hainan.R;
import com.hzpd.hainan.model.newspager.NewsPaper_Bean;
import com.hzpd.hainan.module.newspager.NewsPaperDetailActivity;

import java.util.List;

/**
 * Created by Administrator on 2018/11/19.
 */

public class NewsPaperListAdapter extends RecyclerView.Adapter<NewsPaperListAdapter.MyHolder> {

    private List<NewsPaper_Bean> list;
    private Context context;


    public NewsPaperListAdapter(List<NewsPaper_Bean> list,Context context){
        super();
        this.context = context;
        this.list = list;
    }

    public void setNewList(List<NewsPaper_Bean> list){
        this.list.clear();
        this.list.addAll(list);
    }
    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.szb_list_item,parent,false);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
        Glide.with(context).load(list.get(position).getImg()).placeholder(context.getResources().getDrawable(R.drawable.newspaper)).dontAnimate().into(holder.newpaper_img);
        holder.newpaper_name.setText(list.get(position).getName());
        holder.new_paper_root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,NewsPaperDetailActivity.class);
                intent.putExtra("url",list.get(position).getUrl());
                intent.putExtra("name",list.get(position).getName());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyHolder extends RecyclerView.ViewHolder{
        private ImageView newpaper_img;
        private TextView newpaper_name;
        private LinearLayout new_paper_root;

        public MyHolder(View itemView) {
            super(itemView);
            newpaper_img = itemView.findViewById(R.id.newpaper_img);
            newpaper_name = itemView.findViewById(R.id.newpaper_name);
            new_paper_root = itemView.findViewById(R.id.new_paper_root);
        }
    }
}
